package sol.leetcode;

import java.util.Arrays;
import java.util.Stack;

/**
 * Simplify Path
 * Created by lkk on 2016/8/27.
 */
public class Issue071 {
    public static String simplifyPath(String path) {
        String[] paths = path.split("/");
        StringBuilder sb = new StringBuilder();

        Stack<String> stk = new Stack<String>();
        for (String p : paths) {
            if ("..".equals(p)) {
                if (!stk.empty())
                    stk.pop();
            } else if (".".equals(p))
                ;
            else
                stk.push(p);
        }

        if (stk.size() == 0)
            return "/";

        for (String aStk : stk) {
            sb.append("/");
            sb.append(aStk);
        }

        return sb.toString();
    }

    public static void main(String[] args) {
        String str = "/...";
        System.out.println(simplifyPath(str));
    }
}
